package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;

//学生登录

@WebServlet("/listStudentServlet")
public class listStudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    //创建字符输入流关联读取的对象
        BufferedReader br = new BufferedReader(new FileReader("E:\\c.txt"));
        //创建集合对象保存student对象
        ArrayList<Student> list = new ArrayList<>();
        //循环读取文件中的数据
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter pw = resp.getWriter();
        String line;
        while ((line=br.readLine())!=null){
            Student stu = new Student();
            String[] sp = line.split(",");
            stu.setUsername(sp[0]);
            stu.setAge(Integer.parseInt(sp[1]));
            stu.setScore(Integer.parseInt(sp[2]));
            list.add(stu);
        }
        //将集合存入会话域中
        req.getSession().setAttribute("students",list);
        //重定向到学生列表页面
        resp.sendRedirect("/stu/listStudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
